package com.junmeng.jdemo.recycler.chat.viewstub;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.common.base.BaseActivityDelegate;
import com.example.common.recycler.base.CommonRecyclerAdapter;
import com.junmeng.jdemo.databinding.ActivityChatDemoViewstubBinding;
import com.junmeng.jdemo.recycler.chat.contextmenu.ContextMenuImage;
import com.junmeng.jdemo.recycler.chat.contextmenu.ContextMenuManager;
import com.junmeng.jdemo.recycler.chat.contextmenu.ContextMenuText;
import com.junmeng.jdemo.recycler.chat.contextmenu.MenuInfo;
import com.junmeng.jdemo.recycler.chat.contextmenu.MenuViewManager;
import com.junmeng.jdemo.recycler.chat.contextmenu.TypeManager;
import com.junmeng.jdemo.recycler.chat.contextmenu.base.MenuListener;

/**
 * 模拟聊天页
 */
public class ChatDemoWithViewStubActivity extends BaseActivityDelegate {
    ActivityChatDemoViewstubBinding binding;
    CommonRecyclerAdapter chatAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityChatDemoViewstubBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, RecyclerView.VERTICAL, false);
        binding.recyclerView.setAdapter(chatAdapter = new CommonRecyclerAdapter(new ChatBindViewManager()));
        binding.recyclerView.setLayoutManager(linearLayoutManager);
        binding.recyclerView.addItemDecoration(new DividerItemDecoration(this, RecyclerView.VERTICAL));
        binding.recyclerView.addItemDecoration(new ChatTimeItemDecoration());
//        binding.recyclerView.addItemDecoration(new ChatTimeItemDecoration2());
//        binding.recyclerView.addItemDecoration(new ChatTimeItemDecoration());
//        binding.recyclerView.addItemDecoration(new ChatTimeItemDecoration());
//        binding.recyclerView.addItemDecoration(new ChatTimeItemDecoration());
//        binding.recyclerView.addItemDecoration(new ChatTimeItemDecoration());
        chatAdapter.register(new CommonBindViewText());
        chatAdapter.register(new CommonBindViewImage());
        chatAdapter.register(new CommonBindViewSticker());
        chatAdapter.register(new CommonBindViewSystem());
        chatAdapter.setOnItemClickLitener((v, position, o) -> showToast("click position=" + position));
        chatAdapter.setOnItemLongClickListener((v, position, o) -> {
            showToast("long click position=" + position);
            handleContextMenu(v, position, o);

            return true;
        });

        binding.swipeRefreshLayout.setOnRefreshListener(() -> {
            binding.swipeRefreshLayout.postDelayed((Runnable) () -> {
                final int num = 10;
                for (int i = 0; i < num; i++) {
                    int rand = (int) (Math.random() * 4);
                    int kindRand = (int) (Math.random() * 2);
                    switch (rand) {
                        case 0:
                            chatAdapter.addItemToFront(new CommonBindViewText.Item(kindRand, chatAdapter.getItemCount() + ""));
                            break;
                        case 1:
                            chatAdapter.addItemToFront(new CommonBindViewImage.Item(kindRand, chatAdapter.getItemCount() + ""));
                            break;
                        case 2:
                            chatAdapter.addItemToFront(new CommonBindViewSticker.Item(kindRand, chatAdapter.getItemCount() + ""));
                            break;
                        case 3:
                            chatAdapter.addItemToFront(new CommonBindViewSystem.Item("XXX 撤回了消息"));
                            break;
                    }
                }
                chatAdapter.notifyItemRangeInserted(0, num);
                binding.swipeRefreshLayout.setRefreshing(false);
            }, 1000);//模拟延时1秒

        });

    }

    private void handleContextMenu(View v, int position, Object o) {
        String type = "";
        if (o instanceof CommonBindViewText.Item) {
            type = ContextMenuText.class.getSimpleName();
        } else if (o instanceof CommonBindViewImage.Item) {
            type = ContextMenuImage.class.getSimpleName();
        }

        ContextMenuManager contextMenuManager = new ContextMenuManager();
        contextMenuManager
                .setTypeManager(new TypeManager())
                .setMenuListener(new MenuListener<MenuInfo, Object>() {
                    @Override
                    public void onMenuClick(@Nullable View view, @NonNull MenuInfo menuInfo, @Nullable Object userData) {
                        showToast("menuId=" + menuInfo.menuId + ",userData=" + userData);
                    }
                })
                .setMenuViewManager(new MenuViewManager(this))
                .show(type, type);
    }

    public void onClickSystem(View view) {
        boolean isScrollToBottom = chatAdapter.isScrollToBottom(binding.recyclerView);
        int rand = (int) (Math.random() * 3);
        switch (rand) {
            case 0:
                chatAdapter.addItem(new CommonBindViewSystem.Item("XXX 撤回了消息"));
                break;
            case 1:
                chatAdapter.addItem(new CommonBindViewSystem.Item("XXX 已加入，XXX 和 AAA , BBB 都不是好友关系，请提高防范意识，注意财产安全，谨防上当受骗"));
                break;
            case 2:
                chatAdapter.addItem(new CommonBindViewSystem.Item("XXX 已被禁言"));
                break;
        }
        chatAdapter.notifyItemInserted(chatAdapter.getItemCount() - 1);
        if (isScrollToBottom) {
            chatAdapter.smoothScrollToBottom(binding.recyclerView);
        }
    }

    public void onClickReceive(View view) {
        boolean isScrollToBottom = chatAdapter.isScrollToBottom(binding.recyclerView);
        int rand = (int) (Math.random() * 3);
        switch (rand) {
            case 0:
                chatAdapter.addItem(new CommonBindViewText.Item(0, chatAdapter.getItemCount() + ""));
                break;
            case 1:
                chatAdapter.addItem(new CommonBindViewImage.Item(0, chatAdapter.getItemCount() + ""));
                break;
            case 2:
                chatAdapter.addItem(new CommonBindViewSticker.Item(0, chatAdapter.getItemCount() + ""));
                break;
        }
        chatAdapter.notifyItemInserted(chatAdapter.getItemCount() - 1);
        if (isScrollToBottom) {
            chatAdapter.smoothScrollToBottom(binding.recyclerView);
        }
    }

    public void onClickSend(View view) {
        int rand = (int) (Math.random() * 3);
        switch (rand) {
            case 0:
                chatAdapter.addItem(new CommonBindViewText.Item(1, chatAdapter.getItemCount() + ""));
                break;
            case 1:
                chatAdapter.addItem(new CommonBindViewImage.Item(1, chatAdapter.getItemCount() + ""));
                break;
            case 2:
                chatAdapter.addItem(new CommonBindViewSticker.Item(1, chatAdapter.getItemCount() + ""));
                break;
        }
        chatAdapter.notifyItemInserted(chatAdapter.getItemCount() - 1);
        chatAdapter.smoothScrollToBottom(binding.recyclerView);
    }

    public void onClickBack(View view) {
        onBackPressed();
    }
}